package com.neusoft.actor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.actor.dao.UserDao;
import com.neusoft.actor.po.ResponseBean;
import com.neusoft.actor.po.User;
import com.neusoft.actor.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    @Cacheable(value = "getAllUsers", key = "'all'")
    public ResponseBean<List<User>> getAllUsers() {
        List<User> users = userDao.selectList(null);
        return new ResponseBean<>(users);
    }

    @Override
    @Cacheable(value = "getAllUsers", key = "#userId")
    public ResponseBean<User> getUserById(Integer userId) {
        User user = userDao.selectById(userId);
        if (user != null) {
            return new ResponseBean<>(user);
        } else {
            return new ResponseBean<>(404, "用户未找到");
        }
    }

    @Override
//    按用户名查找用户
    public ResponseBean<User> getUserByName(String userName) {
        List<User> users = userDao.selectList(new QueryWrapper<User>().eq("role", "管家"));
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", userName);
        User user = userDao.selectOne(queryWrapper);
        if (user != null) {
            return new ResponseBean<>(user);
        } else {
            return new ResponseBean<>(404, "用户未找到");
        }
    }
    @Override
//    根据管家姓名模糊查询管家信息
    public ResponseBean<List<User>> getUserByCareTakerName(String careTakerName) {
        // 查询所有角色为“管家”的用户
        List<User> users = userDao.selectList(new QueryWrapper<User>().eq("role", "管家"));

        // 如果提供了管家姓名，则进行模糊查询
        if (careTakerName != null && !careTakerName.isEmpty()) {
            users = users.stream()
                    .filter(user -> user.getUserName().contains(careTakerName))
                    .collect(Collectors.toList());
        }

        // 返回结果
        if (!users.isEmpty()) {
            return new ResponseBean<>(users);
        } else {
            return new ResponseBean<>(404, "用户未找到");
        }
    }

    @Override
    public ResponseBean<Integer> addUser(User user) {
        int result = userDao.insert(user);
        return new ResponseBean<>(result); // 返回插入成功的行数
    }

    @Override
    public ResponseBean<Integer> updateUser(User user) {
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        int result = userDao.updateById(user);
        return new ResponseBean<>(result); // 返回更新成功的行数
    }

    @Override
    public ResponseBean<Integer> deleteUser(Integer userId) {
        int result = userDao.deleteById(userId);
        return new ResponseBean<>(result); // 返回删除成功的行数
    }

    @Override
//    用户登录
    public ResponseBean<User> login(String username, String password) {
        ResponseBean<User> result = this.getUserByName(username);
        ResponseBean<User> rb;
//        密码加密匹配
        String realPassword = DigestUtils.md5DigestAsHex(password.getBytes());
//        System.out.println(realPassword);
        if (result.getStatus() != 200 || !result.getData().getPassword().equals(realPassword)) {
            rb = new ResponseBean<>(500, "账号或密码错误");
        } else {
            result.getData().setPassword(null);
            rb = new ResponseBean<>(result.getData());
        }
        return rb;
    }
}